$(function(){
    purchase.purchaseFun();

    var price = 30; //电影票价
    var $cart = $('#seats_chose'), //座位区

    $tickects_num = $('#tickects_num'), //票数

    $total_price = $('#total_price'); //票价总额
    
    var sc = $('#seat_area').seatCharts({

            map: [//座位结构图 c 代表座位; 下划线 "_" 代表过道

                'cccccccccc',

                'cccccccccc',

                '__________',

                'cccccccc__',

                'cccccccccc',

                'cccccccccc',

                'cccccccccc',

                'cccccccccc',

                'cccccccccc',

                'cc__cc__cc'

            ],

            naming: {//设置行列等信息

                top: false, //不显示顶部横坐标（行） 

                getLabel: function(character, row, column) { //返回座位信息 

                    return column;

                }

            },

            legend: {//定义图例

                node: $('#legend'),

                items: [

                    ['c', 'available', '可选座'],

                    ['c', 'unavailable', '已售出']

                ]

            },

            click: function() {

                if (this.status() == 'available') { //若为可选座状态，添加座位

                    $('<li>' + (this.settings.row + 1) + '排' + this.settings.label + '座</li>')

                            .attr('id', 'cart-item-' + this.settings.id)

                            .data('seatId', this.settings.id)

                            .appendTo($cart);



                    $tickects_num.text(sc.find('selected').length + 1); //统计选票数量

                    $total_price.text(getTotalPrice(sc) + price);//计算票价总金额



                    return 'selected';

                } else if (this.status() == 'selected') { //若为选中状态



                    $tickects_num.text(sc.find('selected').length - 1);//更新票数量

                    $total_price.text(getTotalPrice(sc) - price);//更新票价总金额

                    $('#cart-item-' + this.settings.id).remove();//删除已预订座位



                    return 'available';

                } else if (this.status() == 'unavailable') { //若为已售出状态

                    return 'unavailable';

                } else {

                    return this.style();

                }

            }

        });
        purchase.judgeFun();
        //设置已售出的座位

        // sc.get(['1_3', '1_4', '4_4', '4_5', '4_6', '4_7', '4_8']).status('unavailable');

        function getTotalPrice(sc) { //计算票价总额

            var total = 0;

            sc.find('selected').each(function() {

                total += price;

            });

            return total;

        }
});
    
var purchase = {
    judgeFun:function(){
        var judgeUrl = '/search/orderinfo'; 
        publicDom.post(judgeUrl,'',function(responseData){
            if (responseData.Code === 0) {
                var data = responseData.Data;
                for (var i = 0; i < data.length; i++) {
                    var num = data[i].SeatId;
                    var a1 = num%10;
                    var a2 = parseInt(num/10);
                    if (a2 === 0) {
                        var selectedId = "1_"+a1;
                        $('#'+selectedId).removeClass('available').addClass('unavailable');
                    }
                    else {
                        var a2 = a2+1;
                        var selectedId = a2+"_"+a1;
                        $('#'+selectedId).removeClass('available').addClass('unavailable');
                    }

                }
            }
            else {
                layer.msg('获取已选座位失败');
            }
        },function(responseData){
            layer.msg(responseData.Msg);
        });
    },
    purchaseFun:function(){
        var FlieName = localStorage.getItem('db_FileName');
        var url = window.location.search; //获取url中"?"符后的字串
        var theRequest = {};
        if (url.indexOf("?") !== -1) {
          var str = url.substr(1),
              strs = str.split("&");
          for(var i = 0; i < strs.length; i ++) {
             theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
          }
        }
        var html = $("#booking_area_aa").html();
        html = html.replace('{{name}}',FlieName);
        html = html.replace("{{time}}",theRequest.StartTime);
        $("#booking_area_aa").html(html);
        var purchaseUrl = '/purchase';
        $("#buy_ticket").click(function(){
            var seatNum = $("#seats_chose li").length;
            if (seatNum === 0) {
                layer.msg('请选择座位');
            }
            else {
                var seatids = {};
                $("#seats_chose li").each(function(index,elem){
                    var a = $(this).text();
                    var a1 = parseInt(a[0]);
                    if(a.length === 4){
                        var a2 = parseInt(a[2]);    
                    }
                    else {
                        var a2 = parseInt(a[2]+a[3]);
                    }
                    var total = (a1-1)*10+a2;
                    seatids.seatid = total;
                    publicDom.post(purchaseUrl,seatids,function(responseData){
                        if (responseData.Code === 0) {
                            layer.msg(responseData.Msg);
                        }
                        else {
                            layer.msg(responseData.Msg);
                        }
                    },function(error){
                        layer.msg(error.Msg);
                    });
                });
                
            }
        });
    },
}
